package database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/** handling of users
 * current features:
 * to doesUserExist -> check if user exists in a given table/usernameColumn. **/
public class user
{
	static databaseInteraction DatabaseConnection = new databaseInteraction();

	/** handling credentials of the user of the program **/
	public String ID = ""; // unique number given to every user by auto-increment of mysql
	public String loginUser = "";
	public String loginPassword = "";
	public String passwordColumn = "";
	public String usernameColumn = "";
	public String passwordEncryption = "";
	
	public Boolean userExists = false;
	public Boolean passwordMatches = false;
	
	/** from outside accessible variable, that holds the last result as java processable object **/
	public ArrayList<Object> answerJAVA = new ArrayList<Object>();
	/** from outside accessible variable, that holds the last result (of some functions) as XML string, deliverable and processible by flex/flash player xml decode **/
	public StringBuffer answerXML = new StringBuffer();
	
	/**
	 * check if the user exist in database
	 */
	public Boolean doesUserExist(String loginUser, String loginPassword, String passwordEncryption, database_auth database_auth_inst)
	{
		this.loginUser = loginUser;
		this.loginPassword = loginPassword;
		this.passwordColumn = database_auth_inst.passwordColumn;
		this.usernameColumn = database_auth_inst.usernameColumn;
		this.passwordEncryption = passwordEncryption;
		
		String question = "SELECT * FROM `"+database_auth_inst.DatabaseName+"`.`"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"';"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		
		answerJAVA = DatabaseConnection.answerJAVA;
		
		if(answerJAVA.size() != 0)
		{
			userExists = true;
			
			// 2. check if the password matches
			if(passwordEncryption.equals("clear text") || passwordEncryption.equals("no") || passwordEncryption.equals("false") || passwordEncryption.equals(""))
			{
				question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"' AND `"+passwordColumn+"`='"+loginPassword+"';"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
			}
			else if(passwordEncryption.equals("md5"))
			{
				question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"' AND `"+passwordColumn+"`=PASSWORD('"+loginPassword+"');"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
			}
			
			DatabaseConnection.askDatabase( database_auth_inst, question);
			answerJAVA = DatabaseConnection.answerJAVA;
			
			if(answerJAVA.size() != 0)
			{
				passwordMatches = true;
			}
		}
		else
		{
			userExists = false;
		}
		
		return userExists;
	}
	
	/**
	 * get a list of all existing users (without passwords)
	 */
	public ArrayList allUsers(String loginUser, String loginPassword, String passwordEncryption, database_auth database_auth_inst)
	{
		this.loginUser = loginUser;
		this.loginPassword = loginPassword;
		this.passwordColumn = database_auth_inst.passwordColumn;
		this.usernameColumn = database_auth_inst.usernameColumn;
		this.passwordEncryption = passwordEncryption;
		
		String question = "SELECT * FROM `"+database_auth_inst.authTable+"`;";
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
	
		answerJAVA = DatabaseConnection.answerJAVA;
		
		return answerJAVA;
	}
	
	/**
	 * get a list of all existing users (without passwords) as XML ready to be processed by flex
	 */
	public StringBuffer allUsersXML(String loginUser, String loginPassword, String passwordEncryption, database_auth database_auth_inst)
	{
		this.loginUser = loginUser;
		this.loginPassword = loginPassword;
		this.passwordColumn = database_auth_inst.passwordColumn;
		this.usernameColumn = database_auth_inst.usernameColumn;
		this.passwordEncryption = passwordEncryption;
		
		String question = "SELECT * FROM `"+database_auth_inst.authTable+"`;";
		
		DatabaseConnection.askDatabase(database_auth_inst, question, "password", false);
		
		answerJAVA = DatabaseConnection.answerJAVA;
		answerXML = DatabaseConnection.answerXML;
		
		return answerXML;
	}

	/**
	 * retrieve all informations about the user from the database
	 */
	public ArrayList getInfo(String loginUser, String loginPassword, String passwordEncryption, database_auth database_auth_inst)
	{
		ArrayList<Object> result = new ArrayList<Object>();
		
		this.loginUser = loginUser;
		this.loginPassword = loginPassword;
		this.passwordColumn = database_auth_inst.passwordColumn;
		this.usernameColumn = database_auth_inst.usernameColumn;
		this.passwordEncryption = passwordEncryption;
		
		String question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"';"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		
		answerJAVA = DatabaseConnection.answerJAVA;
		
		if(answerJAVA.size() != 0)
		{
			userExists = true;
			
			// 2. check if the password matches
			if(passwordEncryption.equals("clear text") || passwordEncryption.equals("no") || passwordEncryption.equals("false") || passwordEncryption.equals(""))
			{
				question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"' AND `"+passwordColumn+"`='"+loginPassword+"';"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
			}
			else if(passwordEncryption.equals("md5"))
			{
				question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"' AND `"+passwordColumn+"`=PASSWORD('"+loginPassword+"');"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
			}
			
			DatabaseConnection.askDatabase( database_auth_inst, question);
			result = answerJAVA = DatabaseConnection.answerJAVA;
		}
		
		return result;
	}
	
	/**
	 * retrieve only USERID of the given user from the database
	 */
	public String getID(String loginUser, String loginPassword, String passwordEncryption, database_auth database_auth_inst)
	{
		String question = "";
		this.loginUser = loginUser;
		this.loginPassword = loginPassword;
		this.passwordColumn = database_auth_inst.passwordColumn;
		this.usernameColumn = database_auth_inst.usernameColumn;
		this.passwordEncryption = passwordEncryption;
		
		if(passwordEncryption.equals("clear text") || passwordEncryption.equals("no") || passwordEncryption.equals("false") || passwordEncryption.equals(""))
		{
			question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"' AND `"+passwordColumn+"`='"+loginPassword+"';"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		}
		else if(passwordEncryption.equals("md5"))
		{
			question = "SELECT * FROM `"+database_auth_inst.authTable+"` WHERE `"+usernameColumn+"`='"+loginUser+"' AND `"+passwordColumn+"`=PASSWORD('"+loginPassword+"');"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		}
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, database_auth_inst.authTable, user, password, question);
		
		answerJAVA = DatabaseConnection.answerJAVA;

	    /** iterate over ArrayList **/
	    int LENGTH  =  answerJAVA.size();
	    for(int counter = 0;counter < LENGTH;counter++)
	    {
	    	/** iterate over HashMap **/
	    	HashMap element = (HashMap) answerJAVA.get(counter);
	    	
	    	Set<String> KEYSET = element.keySet();
	    	Object[] KEYS = KEYSET.toArray();
	    	
	    	/** iterate over element **/
	    	for ( Object OBJECT : KEYSET )
	    	{
	    		if(OBJECT.equals("ID"))
	    		{
	    			ID = (String) element.get(OBJECT);
	    			break;
	    		}
	    	}
	    }
        
		return ID;
	}

	
	/** is user ignored?
	 * 
	 * check if USERID is ignored by toUserID
	 * **/
	public boolean ignored(String fromUserID, String toUserID, database_auth database_auth_inst)
	{
		boolean result = true;

		// 2.2. is the receiver ignoring the source?
		// in other words... is there a record in the table groups that has OWNERID = of the receiver, fromUserID = of the sender, and NAME = blockedUser 

		String question = "SELECT * FROM `ignore` WHERE `USERID` = '"+fromUserID+"' AND `IgnoredUserID` = '"+toUserID+"';";
		// String question = "SELECT * FROM `groups` WHERE `OWNERID` = "+fromUserID+" AND `USERID` = "+toUserID+" AND `NAME` = 'blockedUser';";
		DatabaseConnection.askDatabase(database_auth_inst, question, "password", false);

		if(DatabaseConnection.answerJAVA.size() >= 1)
		{
			// yes: don't write a record
			result = true;
		}
		else
		{
			// no:
			// 2.3. write a record with receiverUser with the username of the receiving user.
			result = false;
		}
		
		return result;
	}
}

